<?php
namespace app\api\controller\v1;

class Collect extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //我的收藏
    public function lists(){
        $d = $this->post();
        $order = 0;
        $page = isset($d['page']) ? (int)$d['page'] : 1;
        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];

        $list = m("UserCollect","c")->lists($map,0, $page,5);
        //商品列表推荐位5条数据
        $list = [
            "list"=>$list[0],
            "page_count"=>$list[1],
            'max_page' => ceil($list[1] / 5),
            "page" => $page
        ];
        suc($list);
    }

    //添加收藏
    public function add(){
        $goodsid = $this->post('goodsid');
        $info = $this->db->query('select * from goods_collect where goods_id=? and user_id=?',[$goodsid,$this->userinfo["id"]]);
        if($info){
            return suc();
        }
        $data = [
            'goods_id' => $goodsid,
            'user_id' => $this->userinfo["id"],
            'create_time' => time(),
        ];
        $re = $this->db->insert('goods_collect',$data);
        if($re){
            return suc();
        }else{
            err('收藏失败!');
        }

    }

    //删除收藏
    public function del(){
        $id = $this->post("id");
        $info = $this->db->query('select * from goods_collect where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            $re = $this->db->delete('goods_collect',$id,'id');
            if($re){
                return suc();
            }else{
                err('删除失败!');
            }
        }else{
            err('删除失败!');
        }
    }

}
?>
